<template>
  <a-checkbox-group :options="options" :defaultValue="getDefaultValue()" @change="changeHandle" />
</template>

<script setup>
  import { ref, onMounted } from 'vue';
  import { setCellValue, getCellValue } from '../utils/tools';

  const props = defineProps({
    options: { type: Array },
    cell: { type: Object },
    editor: { type: Object },
  });
  const bodyTypeValue = ref();
  const emit = defineEmits(['change', 'input']);
  onMounted(() => {
    bodyTypeValue.value = getCellValue(props, 'body-type', 'none');
  });
  function getDefaultValue(){
    let selectValues = [];
    props.options.forEach((element) => {
        if (getCellValue(props, element.value, element.defaultVal) === '1') {
          selectValues.push(element.value);
          setCellValue(props, element.value, '1');
        } else {
          setCellValue(props, element.value, '0');
        }
      });
    emit('change', selectValues);
    // emit('input', selectValues);
    return selectValues;
  }
  // 处理请求设置选中的值和初始化值
  function changeHandle(selectValues) {
    props.options.forEach((element) => {
        if (selectValues.indexOf(element.value) === -1) {
          setCellValue(props, element.value, '0');
        } else {
          setCellValue(props, element.value, '1');
        }
      });
      emit('change', selectValues);
      emit('input', selectValues);
  }
</script>
